package jianzhi

func getIntersectionNode(headA, headB *ListNode) *ListNode {

	la, lb := helper52(headA), helper52(headB)

	tempA := headA
	tempB := headB

	if la < lb {
		for i := 0; i < lb-la; i++ {
			tempB = tempB.Next
		}
	} else if la > lb {
		for i := 0; i < la-lb; i++ {
			tempA = tempA.Next
		}
	}

	for tempA != nil && tempB != nil && tempA != tempB {
		tempA = tempA.Next
		tempB = tempB.Next
	}

	return tempA

}

// 获取链表长度
func helper52(l *ListNode) int {

	n := 0
	for temp := l; temp != nil; temp = temp.Next {
		n++
	}

	return n

}
